package com.example.service;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.Constants;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Admin;
import com.example.entity.Helper;
import com.example.entity.User;
import com.example.exception.CustomException;
import com.example.mapper.AdminMapper;
import com.example.mapper.HelperMapper;
import com.example.mapper.UserMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class HelperService {


    @Autowired
    private HelperMapper helperMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 登录
     */
    public Account login(Account account) {
        Account helper  = helperMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(helper)) {
            throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
        }
        if (!account.getPassword().equals(helper.getPassword())) {
            throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
        }
        // 生成token
        String tokenData = helper.getId() + "-" + RoleEnum.HELPER.name();
        String token = TokenUtils.createToken(tokenData, helper.getPassword());
        helper.setToken(token);
        return helper;
    }

    /**
     * 分页查询
     */
    public PageInfo<User> selectPage(User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        String username = TokenUtils.getCurrentUser().getUsername();
        List<User> list = userMapper.selectAll(user);
        return PageInfo.of(list);
    }
    /**
     * 根据ID查询
     */
    public Helper selectById1(Integer id) {
        Helper user = helperMapper.selectById(id);
        // 生成token
        String tokenData = user.getId() + "-" + RoleEnum.USER.name();
        String token = TokenUtils.createToken(tokenData, user.getPassword());
        user.setToken(token);
        return user;
    }


    /**
     * 新增
     */
    public void add(Admin admin) {
        Helper dbAdmin = helperMapper.selectByUsername(admin.getUsername());
        if (ObjectUtil.isNotNull(dbAdmin)) {
            throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
        }
        if (ObjectUtil.isEmpty(admin.getPassword())) {
            admin.setPassword(Constants.USER_DEFAULT_PASSWORD);
        }
        if (ObjectUtil.isEmpty(admin.getName())) {
            admin.setName(admin.getUsername());
        }
        admin.setRole(RoleEnum.ADMIN.name());
        helperMapper.insert(admin);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        helperMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            helperMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Admin admin) {
        helperMapper.updateById(admin);
    }

    /**
     * 根据ID查询
     */
    public Helper selectById(Integer id) {
        return helperMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Helper> selectAll(Admin admin) {
        return helperMapper.selectAll(admin);
    }

    /**
     * 分页查询
     */
    public PageInfo<Helper> selectPage1(Admin admin, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Helper> list = helperMapper.selectAll(admin);
        return PageInfo.of(list);
    }
}
